//
//  MonitorInfoSecondView.m
//  ICrEAM
//
//  Created by kuroro on 16/11/2.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "MonitorInfoSecondView.h"
#import "ActivityTableViewCell.h"
#import "MonitorInfoSecondTableViewCell.h"

@interface MonitorInfoSecondView()
@property (nonatomic,strong) NSMutableArray *activitvArray;
@property (nonatomic,strong) NSMutableArray *repairArray;
@property (nonatomic,strong) NSMutableArray *partArray;
@end

@implementation MonitorInfoSecondView

-(NSMutableArray *)activitvArray{
    if (!_activitvArray) {
        _activitvArray = [NSMutableArray array];
    }
    return _activitvArray;
}

-(NSMutableArray *)repairArray{
    if (!_repairArray) {
        _repairArray = [NSMutableArray array];
    }
    return _repairArray;
}

-(NSMutableArray *)partArray{
    if (!_partArray) {
        _partArray = [NSMutableArray array];
    }
    return _partArray;
}

- (instancetype)initMonitorInfoSecondViewWithDic:(NSDictionary *)dic{
    if (self = [super init]) {
        
        if (IsArr(dic[@"TT_ACTIVITY"])) {

            [self.activitvArray addObjectsFromArray:dic[@"TT_ACTIVITY"]];
        }
        if (IsArr(dic[@"TT_BOOKING_ORDER_PART"])) {
            [self.partArray addObjectsFromArray:dic[@"TT_BOOKING_ORDER_PART"]];
        }
        if (IsArr(dic[@"TT_REPAIR_ORDER"])) {
            [self.repairArray addObjectsFromArray:dic[@"TT_REPAIR_ORDER"]];
        }
        
        __weak typeof(self) weakSelf = self;
        self.backgroundColor = [UIColor whiteColor];
        //头部
        UIView *headerView = [[UIView alloc]init];
        
        UILabel *titleLabel = [[UILabel alloc]init];
        titleLabel.font = TitleFont;
        titleLabel.textColor = TitleColor;
        titleLabel.text = @"监控信息";
        [headerView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(headerView.mas_centerX);
            make.centerY.equalTo(headerView.mas_centerY);
        }];
        
        UILabel *pageNum = [[UILabel alloc]init];
        pageNum.font = ContentFont;
        pageNum.textColor = ContentTextColor_Gray;
        pageNum.text = @"(2/3)";
        [headerView addSubview:pageNum];
        [pageNum mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(titleLabel.mas_bottom);
            make.left.equalTo(titleLabel.mas_right);
        }];
        
        UIView *headerLineView = [[UIView alloc]init];
        headerLineView.backgroundColor = SepColor_gray;
        [headerView addSubview:headerLineView];
        [headerLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(headerView.mas_left).offset(3);
            make.right.equalTo(headerView.mas_right).offset(-3);
            make.bottom.equalTo(headerView.mas_bottom);
            make.height.equalTo(@(1));
        }];
        
        [self addSubview:headerView];
        [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.mas_top);
            make.left.equalTo(weakSelf.mas_left);
            make.right.equalTo(weakSelf.mas_right);
            make.height.equalTo(@(ActualHeight(65)));
        }];
        
        //活动
        UIView *activityView = [[UIView alloc]init];
        activityView.backgroundColor = BackgroundColor_White;
        
        UILabel *activityLabel = [[UILabel alloc]init];
        activityLabel.font = NaviFont;
        activityLabel.textColor = TitleColor_Dark;
        activityLabel.text = @"服务活动";
        [activityView addSubview:activityLabel];
        [activityLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(activityView.mas_left).offset(ActualWidth(60));
            make.top.equalTo(activityView.mas_top).offset(ActualHeight(30));
        }];
        
        self.activityTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        self.activityTableView.delegate = self;
        self.activityTableView.dataSource = self;
        self.activityTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.activityTableView.backgroundColor = BackgroundColor_White;
        [self.activityTableView registerClass:[ActivityTableViewCell class] forCellReuseIdentifier:@"ActivityTableViewCell"];
        [activityView addSubview:self.activityTableView];
        [self.activityTableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(activityLabel.mas_bottom).offset(15);
            make.centerX.equalTo(activityView.mas_centerX);
            make.width.equalTo(@(ActualWidth(760)));
            make.height.equalTo(@(ActualHeight(120)));
        }];
        
        UIView *activityLine = [[UIView alloc]init];
        activityLine.backgroundColor = SepColor_gray;
        [activityView addSubview:activityLine];
        [activityLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(activityView.mas_bottom);
            make.left.equalTo(activityView.mas_left).offset(3);
            make.right.equalTo(activityView.mas_right).offset(-3);
            make.height.equalTo(@(1));
        }];
        
        [self addSubview:activityView];
        [activityView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(headerView.mas_bottom);
            make.left.equalTo(weakSelf.mas_left);
            make.right.equalTo(weakSelf.mas_right);
            make.height.equalTo(@(ActualHeight(190)));
        }];
        
        
        //委托书
        UIView *proxy = [[UIView alloc]init];
        proxy.backgroundColor = BackgroundColor_White;
        
        UILabel *proxyLabel = [[UILabel alloc]init];
        proxyLabel.font = NaviFont;
        proxyLabel.textColor = TitleColor_Dark;
        proxyLabel.text = @"未结算的委托书";
        [proxy addSubview:proxyLabel];
        [proxyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(proxy.mas_left).offset(ActualWidth(60));
            make.top.equalTo(proxy.mas_top).offset(ActualHeight(20));
        }];

        UIView *proxyView = [[UIView alloc]init];
        proxyView.layer.borderColor = SepColor_gray.CGColor;
        proxyView.backgroundColor = [UIColor whiteColor];
        proxyView.layer.borderWidth = 1;
        
        UIView *proxyHeaderView = [self SetUpHeaderViewWithID:@"委托书号" time:@"开具日期" type:@"维修类型"];
        [proxyView addSubview:proxyHeaderView];
        [proxyHeaderView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(proxyView.mas_left).offset(1);
            make.right.equalTo(proxyView.mas_right).offset(-1);
            make.top.equalTo(proxyView.mas_top).offset(1);
            make.height.equalTo(@(ActualHeight(40)));
        }];
        
        self.proxyTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        self.proxyTableView.delegate = self;
        self.proxyTableView.dataSource = self;
        self.proxyTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        [self.proxyTableView registerClass:[MonitorInfoSecondTableViewCell class] forCellReuseIdentifier:@"MonitorInfoSecondTableViewCell"];
        [proxyView addSubview:self.proxyTableView];
        [self.proxyTableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(proxyHeaderView.mas_bottom).offset(-1);
            make.bottom.equalTo(proxyView.mas_bottom);
            make.left.equalTo(proxyView.mas_left).offset(1);
            make.right.equalTo(proxyView.mas_right).offset(-1);
        }];

        [proxy addSubview:proxyView];
        [proxyView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(proxyLabel.mas_bottom).offset(15);
            make.centerX.equalTo(proxy.mas_centerX);
            make.width.equalTo(@(ActualWidth(760)+2));
            make.height.equalTo(@(ActualHeight(100)+2));
        }];
        
        UIView *proxyLineView = [[UIView alloc]init];
        proxyLineView.backgroundColor = SepColor_gray;
        [proxy addSubview:proxyLineView];
        [proxyLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(proxy.mas_left).offset(3);
            make.right.equalTo(proxy.mas_right).offset(-3);
            make.bottom.equalTo(proxy.mas_bottom).offset(-1);
            make.height.equalTo(@(1));
        }];
        
        [self addSubview:proxy];
        [proxy mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(activityView.mas_bottom);
            make.left.equalTo(weakSelf.mas_left);
            make.right.equalTo(weakSelf.mas_right);
            make.height.equalTo(@(ActualHeight(190)));
        }];

        //零件
        UIView *part = [[UIView alloc]init];
        part.backgroundColor = BackgroundColor_White;
        
        UILabel *partLabel = [[UILabel alloc]init];
        partLabel.font = NaviFont;
        partLabel.textColor = TitleColor_Dark;
        partLabel.text = @"预留零件";
        [part addSubview:partLabel];
        [partLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(part.mas_left).offset(ActualWidth(60));
            make.top.equalTo(part.mas_top).offset(ActualHeight(20));
        }];
        
        UIView *partView = [[UIView alloc]init];
        partView.layer.borderColor = SepColor_gray.CGColor;
        partView.backgroundColor = [UIColor whiteColor];
        partView.layer.borderWidth = 1;
        
        UIView *partHeaderView = [self SetUpHeaderViewWithID:@"零件代码" time:@"零件名称" type:@"数量"];
        [partView addSubview:partHeaderView];
        [partHeaderView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(partView.mas_left).offset(1);
            make.right.equalTo(partView.mas_right).offset(-1);
            make.top.equalTo(partView.mas_top).offset(1);
            make.height.equalTo(@(ActualHeight(40)));
        }];
        
        self.partTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        self.partTableView.delegate = self;
        self.partTableView.dataSource = self;
        self.partTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        [self.partTableView registerClass:[MonitorInfoSecondTableViewCell class] forCellReuseIdentifier:@"MonitorInfoSecondTableViewCell"];
        [partView addSubview:self.partTableView];
        [self.partTableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(partHeaderView.mas_bottom).offset(-1);
            make.bottom.equalTo(partView.mas_bottom);
            make.left.equalTo(partView.mas_left).offset(1);
            make.right.equalTo(partView.mas_right).offset(-1);
        }];
        
        [part addSubview:partView];
        [partView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(partLabel.mas_bottom).offset(15);
            make.centerX.equalTo(part.mas_centerX);
            make.width.equalTo(@(ActualWidth(760)+2));
            make.height.equalTo(@(ActualHeight(100)+2));
        }];
        
        [self addSubview:part];
        [part mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(proxy.mas_bottom);
            make.left.equalTo(weakSelf.mas_left);
            make.right.equalTo(weakSelf.mas_right);
            make.bottom.equalTo(weakSelf.mas_bottom);
        }];

        
        

    }
    return self;
}
+ (instancetype)creatMonitorInfoSecondViewWithDic:(NSDictionary *)dic{
    return [[MonitorInfoSecondView alloc]initMonitorInfoSecondViewWithDic:dic];
}

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == self.activityTableView) {
        return self.activitvArray.count;
    }else if(tableView == self.proxyTableView){
        return self.repairArray.count;
    }else{
        return self.partArray.count;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.activityTableView) {
        NSDictionary *dic = self.activitvArray[indexPath.row];
         ActivityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ActivityTableViewCell"];
        [cell SetUpHeaderViewWithTitle:dic];
        return cell;
    }else if (tableView == self.proxyTableView){
        NSDictionary *dic = self.repairArray[indexPath.row];
        MonitorInfoSecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MonitorInfoSecondTableViewCell"];
        [cell SetUpHeaderViewWithID:dic[@"RO_NO"]?:@"" time:dic[@"RO_CREATE_DATE"]?:@"" type:dic[@"RO_REPAIR_TYPE_NAME"]?:@""];
        return cell;
    }else{
        NSDictionary *dic = self.partArray[indexPath.row];
        MonitorInfoSecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MonitorInfoSecondTableViewCell"];
        [cell SetUpHeaderViewWithID:dic[@"PART_NO"]?:@"" time:dic[@"PART_NAME"]?:@"" type:dic[@"OBLIGATED_QUANTITY"]?[NSString stringWithFormat:@"%ld",[dic[@"OBLIGATED_QUANTITY"] integerValue]]:@"0"];
        return cell;
    }
   
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.activityTableView) {
        return ActualHeight(40);
    }else{
        return ActualHeight(30);
    }
    
}


- (UIView *)SetUpHeaderViewWithID:(NSString *)ID time:(NSString *)time type:(NSString *)type {
    UIView *headerView = [[UIView alloc]init];
    headerView.backgroundColor = TitleColor;
    UILabel *IDLabel = [self createLabelWithText:ID textAlignment:NSTextAlignmentCenter];
    [headerView addSubview:IDLabel];
    [IDLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headerView.mas_top);
        make.left.equalTo(headerView.mas_left);
        make.bottom.equalTo(headerView.mas_bottom);
        make.width.equalTo(@(ActualWidth(280)));
    }];
    UIView *lineView1 = [[UIView alloc]init];
    lineView1.backgroundColor = SepColor_gray;
    [headerView addSubview:lineView1];
    [lineView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headerView.mas_top);
        make.left.equalTo(IDLabel.mas_right).offset(-1);
        make.bottom.equalTo(headerView.mas_bottom);
        make.width.equalTo(@(1));
    }];
    
    UILabel *timeLabel = [self createLabelWithText:time textAlignment:NSTextAlignmentCenter];
    [headerView addSubview:timeLabel];
    [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headerView.mas_top);
        make.left.equalTo(IDLabel.mas_right);
        make.bottom.equalTo(headerView.mas_bottom);
        make.width.equalTo(@(ActualWidth(280)));
    }];
    
    UIView *lineView2 = [[UIView alloc]init];
    lineView2.backgroundColor = SepColor_gray;
    [headerView addSubview:lineView2];
    [lineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headerView.mas_top);
        make.left.equalTo(timeLabel.mas_right).offset(-1);
        make.bottom.equalTo(headerView.mas_bottom);
        make.width.equalTo(@(1));
    }];
    
    
    
    UILabel *typeLabel = [self createLabelWithText:type textAlignment:NSTextAlignmentCenter];
    [headerView addSubview:typeLabel];
    [typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headerView.mas_top);
        make.left.equalTo(timeLabel.mas_right);
        make.bottom.equalTo(headerView.mas_bottom);
        make.right.equalTo(headerView.mas_right);
    }];
    

    
    return headerView;
}

- (UILabel *)createLabelWithText:(NSString *)text  textAlignment:(NSTextAlignment)textAlignment{
    UILabel *label = [[UILabel alloc] init];
    label.text = text;
    label.font = ContentFont;
    label.textColor = ContentTextColor_Black;
    label.textAlignment = textAlignment;
    return label;
}


@end
